# -*- coding: utf-8 -*-
from configparser import ConfigParser
from os.path import isfile


class ReadConfig(object):
    def __init__(self, file_path):
        super(ReadConfig, self).__init__()
        self.config = self.read_file(file_path)

    def read_file(self, file_path):
        config = ConfigParser()
        if isfile(file_path): config.read(file_path)
        else:
            config['LOGGING'] = {'level': 'INFO', 'mode': 'a', 'name': 'super_email_sender.log'}
            config['EMAIL'] = {'host': 'smtp.exmail.qq.com', 'port': 465, 'timeout': 60, 'account': '', 'passwd': '',
                               'connection': True, 'validate_certs': True, 'retry': 2}
            config['FORMAT'] = {'format_design': True}
            self.save_file(file_path, config)
        return config

    def save_file(self, file_path, config):
        with open(file_path, 'w') as fp:
            config.write(fp)

